package cn.bocweb.spinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CompanyList1Bean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.activity.CompanyInfoActivity;
import cn.bocweb.spinning.view.adapter.CompanyListAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/3/9.
 */
public class CompanyListFragment extends Fragment {

    SwipeRefreshHelper swipeRefreshHelper;
    CompanyListAdapter adapter;
    List<CompanyList1Bean.DataBean> companyList1BeanList;
    UserPresenter presenter;
    String userid;
    int total;
    LoadingDialog dialog;

    @Bind(R.id.company_list)
    GridView companyList;
    @Bind(R.id.company_list_refresh)
    SwipeRefreshLayout companyListRefresh;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_company_list, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        companyList1BeanList = new ArrayList<>();
        adapter = new CompanyListAdapter(getActivity(), companyList1BeanList);
        presenter = new UserPresenterImp();
        dialog = new LoadingDialog(getActivity());

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(getActivity());
        userid = dataEntity.getUserid();

        initList();
        dialog.show();

        presenter.getCompanyList1("1", userid);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(CompanyList1Bean bean) {
        dialog.hide();
        if (bean.getReturn_code() == 0) {
            companyList1BeanList.addAll(bean.getData());
            adapter.notifyDataSetChanged();
        }
    }

    private void initList() {
        companyList.setAdapter(adapter);
        swipeRefreshHelper = new SwipeRefreshHelper(companyListRefresh, companyList, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                companyList1BeanList.clear();
                presenter.getCompanyList1("1", userid);
            }

            @Override
            public void onLoad() {

            }
        });
        companyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), CompanyInfoActivity.class);
                intent.putExtra(Constant.TO_COMPANY_COLLECT, companyList1BeanList.get(position).getCollect());
                intent.putExtra(Constant.TO_COMPANY, companyList1BeanList.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
